//
//  zipHandler.m
//  gdemm
//
//  Created by 李园园 on 17/3/10.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "zipHandler.h"
#import "zipTableViewController.h"
#import "ZipArchive.h"
#import "FileInfo.h"
@interface zipHandler()
@property(nonatomic,strong)NSMutableArray * fileNameArray;

@end

@implementation zipHandler
-(NSMutableArray *)fileNameArray{
    if (!_fileNameArray) {
        _fileNameArray = [NSMutableArray array];
    }
    return _fileNameArray;
}

- (BOOL)isSupportedType:(NSString *)suffix{
    
    return  [suffix caseInsensitiveCompare:@"zip"]  == NSOrderedSame ;
}

- (UIViewController *)getControllerForFilePath:(NSString *)filePath{
    [self.fileNameArray removeAllObjects];
    //解压
    ZipArchive* zip = [[ZipArchive alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    
    NSString * fileDict = [filePath componentsSeparatedByString:@"/"].lastObject;
    NSString* unzipto = [documentpath stringByAppendingString:[NSString stringWithFormat:@"/%@",fileDict]] ;
    if( [zip UnzipOpenFile:filePath] )
    {
        [zip UnzipFileTo:unzipto overWrite:YES];
        [zip UnzipCloseFile];
    }
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager]enumeratorAtPath:unzipto];
    for (NSString *fileNameStr in enumerator)
    {
        if (![fileNameStr containsString:@"__MACOSX"]) {
             FileInfo * fileInfo = [[FileInfo alloc]init];
            fileInfo.fileName = fileNameStr;
            fileInfo.localPath = [unzipto stringByAppendingPathComponent:fileNameStr];
            [self.fileNameArray addObject:fileInfo];
        }
        
    }
    
    zipTableViewController * table  = [[zipTableViewController alloc]initWithStyle:UITableViewStylePlain];
    table.fileArr = self.fileNameArray;

    return table;
}
- (NSString *)getIconWithFileName:(NSString *)fileName enced:(BOOL)isEnced{
    NSString *icon;
    icon = isEnced ? @"cloudDisk_icon_zip_enced" : @"cloudDisk_icon_zip";
    return icon;
}

@end
